//
//  DXMainViewController.swift
//  nineteenfloor
//
//  Created by 大郗 on 16/3/6.
//  Copyright © 2016年 大郗. All rights reserved.
//

import UIKit
import SnapKit

class DXMainViewController: UITabBarController {
	/// 设置tabbar轮廓线的颜色
	override func viewWillAppear(animated: Bool) {
		self.tabBar.backgroundImage = self.createImageWithColor(UIColor(white: 250 / 255, alpha: 1))
		self.tabBar.shadowImage = self.createImageWithColor(UIColor(white: 230 / 255, alpha: 1))
	}

	func createImageWithColor(color: UIColor) -> UIImage {
		let rect = CGRectMake(0, 0, 1, 1)
		UIGraphicsBeginImageContext(rect.size)
		let context = UIGraphicsGetCurrentContext()
		CGContextSetFillColorWithColor(context, color.CGColor)
		CGContextFillRect(context, rect)
		let image = UIGraphicsGetImageFromCurrentImageContext()
		UIGraphicsEndImageContext()
		return image
	}

	override func viewDidLoad() {
		super.viewDidLoad()
		let tab = DXTabBar()
		setValue(tab, forKey: "tabBar")
		setupUI()
		addChildViewControllers()
	}

	/// 添加控制器
	///
	/// - parameter vc       : 视图控制器
	/// - parameter title    : 标题
	/// - parameter imageName: 图像名称
	private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
		// 设置标题
		vc.title = title
		// 设置图片
		vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
		vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_h")?.imageWithRenderingMode(.AlwaysOriginal)
		// 文字颜色的属性
		let textColorAttr = [
			NSForegroundColorAttributeName: UIColor.clearColor()
		]
		// 设置选中文字颜色
		vc.tabBarItem.setTitleTextAttributes(textColorAttr, forState: UIControlState.Normal)
		vc.tabBarItem.setTitleTextAttributes(textColorAttr, forState: UIControlState.Selected)
		// 使用导航控制器包裹起来
		let nav = UINavigationController(rootViewController: vc)
		addChildViewController(nav)
	}

	/// 添加所有子控制器
	private func addChildViewControllers() {
		addChildViewController(DXMessageViewController(), title: "精选", imageName: "tab_ico1")
		addChildViewController(DXHomeViewController(), title: "社区", imageName: "tab_ico2")
		addChildViewController(DXDiscoverViewController(), title: "发现", imageName: "tab_ico4")
		addChildViewController(DXProfileViewController(), title: "我的", imageName: "tab_ico5")
	}
    
        private func loadSubViewContollerWithSBName(name: String) -> UIViewController {
            let messageStoryboard = UIStoryboard(name: name, bundle: nil)
            return messageStoryboard.instantiateInitialViewController()!
        }

	private func setupUI() {
		view.addSubview(composeButton)
		// 添加约束
		composeButton.snp_makeConstraints { (make) -> Void in
			make.centerX.equalTo(view)
			make.bottom.equalTo(view)
		}
	}

	@objc private func composeButtonClick() {
		print("相机按钮点击了")
	}

	/// 创作按钮 - 懒加载
	lazy var composeButton: UIButton = {
		let button = UIButton()
		// 给按钮添加点击事件
		button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
		// 设置按钮不同状态的图片与背景图片
		button.setBackgroundImage(UIImage(named: "tab_ico3"), forState: UIControlState.Normal)
		button.setBackgroundImage(UIImage(named: "tab_ico3_h"), forState: UIControlState.Highlighted)
		// 设置大小
		button.sizeToFit()
		return button
	}()
}
